package com.wms.oms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wms.common.core.utils.DateUtils;
import com.wms.oms.domain.Payment;
import com.wms.oms.domain.PaymentItem;
import com.wms.oms.enums.ApprovalStatus;
import com.wms.oms.mapper.PaymentItemMapper;
import com.wms.oms.mapper.PaymentMapper;
import com.wms.oms.service.IPaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author zzm
 * @date 2023/8/24
 * @desc
 */
@Service
public class PaymentServiceImpl implements IPaymentService {

    @Autowired
    private PaymentMapper paymentMapper;

    @Autowired
    private PaymentItemMapper paymentItemMapper;

    @Override
    public Payment selectPaymentById(Long id) {
        return paymentMapper.selectById(id);
    }

    @Override
    public List<Payment> selectPaymentList(Payment payment) {
        return paymentMapper.selectList(new LambdaQueryWrapper<>());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertPayment(Payment payment) {
        payment.setSn("PA"+ DateUtils.getNowDate().getTime());
        payment.setPayStatus(ApprovalStatus.WAIT_COMMIT.getValue());
        List<PaymentItem> paymentItemList = payment.getPaymentItemList();
        Assert.notEmpty(paymentItemList, "付款明细不能为空!");
        BigDecimal payAmount = paymentItemList.stream().map(PaymentItem::getAmount).reduce(BigDecimal::add).orElse(BigDecimal.ZERO);
        payment.setPayAmount(payAmount);
        int result = paymentMapper.insert(payment);
        for (PaymentItem item : paymentItemList){
            item.setPaymentId(payment.getId());
            paymentItemMapper.insert(item);
        }
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updatePayment(Payment payment) {
        List<PaymentItem> paymentItemList = payment.getPaymentItemList();
        Assert.notEmpty(paymentItemList, "付款明细不能为空!");
        BigDecimal payAmount = paymentItemList.stream().map(PaymentItem::getAmount).reduce(BigDecimal::add).orElse(BigDecimal.ZERO);
        payment.setPayAmount(payAmount);
        int result = paymentMapper.updateById(payment);
        for (PaymentItem item : paymentItemList){
            if (ObjectUtils.isEmpty(item.getId())) {
                item.setPaymentId(payment.getId());
                paymentItemMapper.insert(item);
            }else {
                paymentItemMapper.updateById(item);
            }
        }
        return result;
    }

    @Override
    public int confirm(Payment payment) {
        Payment pay = paymentMapper.selectById(payment.getId());
        Assert.notNull(pay, "付款单不存在!");
        payment.setPayStatus(ApprovalStatus.APPROVALED.getValue());
        return paymentMapper.updateById(payment);
    }

    @Override
    public int commit(Long id) {
        Payment payment = paymentMapper.selectById(id);
        Assert.notNull(payment, "付款单不存在!");
        payment.setPayStatus(ApprovalStatus.APPROVALING.getValue());
        return paymentMapper.updateById(payment);
    }

    @Override
    public int deletePaymentByIds(Long[] ids) {
        Payment model = new Payment();
        model.setDelFlag(Boolean.TRUE);
        return paymentMapper.update(model, new LambdaQueryWrapper<Payment>().in(Payment::getId, ids));
    }
}
